package common

import (
	"bufio"
	"log"
	"net"
)

type Connection struct {
	net.Conn
	rw *bufio.ReadWriter
}

func BuildConnection(conn net.Conn) *Connection {
	rw := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
	return &Connection{
		conn, rw,
	}
}

func (c *Connection) RxData() bool {
	_, err := c.rw.Peek(1)
	if err == nil {
		return true
	}
	log.Println(err)
	return false
}

func (c *Connection) Read(p []byte) (n int, err error) {
	return c.rw.Read(p)
}

func (c *Connection) Write(p []byte) (nn int, err error) {
	return c.Conn.Write(p)
}
